package com.lujianfei.powersearch.utils

object DatabindingDiffUtils {

    fun <M> setData(oldList: MutableList<M>, newList: List<M>) {
        val newOldOffsetLen = newList.size - oldList.size

        val minSize = Math.min(oldList.size, newList.size)
        for (idx in 0 until minSize) {
            oldList[idx] = newList[idx]
        }
        if (newOldOffsetLen > 0) {
            oldList.addAll(newList.subList(oldList.size, newList.size))
        } else if (newOldOffsetLen < 0) {
            for (idx in oldList.size - 1 downTo newList.size) {
                oldList.removeAt(idx)
            }
        }
    }
}